home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / larn.lha / iventory.c < prev    next >
C/C++ Source or Header  |  1995-11-19  |  13KB  |  485 lines

  1. #include "header.h"
  2. #include "larndefs.h"
  3. #include "objects.h"
  4. #include "player.h"
  5.  
  6. #ifdef __STDC__
  7.         show1( int, char*[] );
  8.             show3( int );
  9. static      show2( int );
  10. static void t_setup( int );
  11. static void t_endup( int );
  12.  
  13. #define SIGNED signed
  14. #else
  15.         show1( );
  16.             show3( );
  17. static      show2( );
  18. static void t_setup( );
  19. static void t_endup( );
  20.  
  21. #define SIGNED
  22. #endif
  23.  
  24. static int  qshowstr();
  25. showwear();
  26. showwield();
  27. showread();
  28. showquaff();
  29. showeat();
  30. extern int dropflag;
  31.  
  32. /* Allow only 26 items (a to z) in the player's inventory */
  33. #define MAXINVEN 26
  34.  
  35. /* The starting limit to the number of items the player can carry.  
  36.    The limit should probably be based on player strength and the
  37.    weight of the items.
  38. */
  39. #define MIN_LIMIT 15
  40.  
  41. /* define a sentinel to place at the end of the sorted inventory.
  42.    (speeds up display reads )
  43. */
  44. #define END_SENTINEL 255
  45.  
  46. /* declare the player's inventory.  These should only be referenced
  47.    in this module.
  48.     iven     - objects in the player's inventory
  49.     ivenarg  - attribute of each item ( + values, etc )
  50.     ivensort - sorted inventory (so we don't sort each time)
  51. */
  52. char iven[MAXINVEN];
  53. SIGNED  short ivenarg[MAXINVEN];
  54. unsigned char ivensort[MAXINVEN+1];    /* extra is for sentinel */
  55.  
  56. static char srcount = 0 ; /* line counter for showstr() */
  57.  
  58. /*
  59.   Initialize the player's inventory
  60. */
  61. void init_inventory( )
  62.     {
  63.     int i;
  64.  
  65.     for ( i = 0; i < MAXINVEN ; i++ )
  66.         {
  67.     iven[i] = ivenarg[i] = 0;
  68.     ivensort[i] = END_SENTINEL ;
  69.     }
  70.     ivensort[MAXINVEN] = END_SENTINEL;
  71.  
  72.     /* For zero difficulty games, start the player out with armor and weapon.
  73.        We can sort the inventory right away because a dagger is 'later' than
  74.        leather armor.
  75.     */
  76.     if (c[HARDGAME] <= 0)
  77.     {
  78.     iven[0] = OLEATHER;
  79.     iven[1] = ODAGGER;
  80.     ivenarg[0] = ivenarg[1] = c[WEAR] = ivensort[0] = 0;
  81.     ivensort[1] = c[WIELD] = 1;
  82.     }
  83.     }
  84.  
  85. /*
  86.     show character's inventory
  87. */
  88. showstr(select_allowed)
  89. char select_allowed;
  90.     {
  91.     register int i,number, item_select;
  92.  
  93.     for (number=3, i=0; i<MAXINVEN; i++)
  94.         if (iven[i])
  95.             number++;  /* count items in inventory */
  96.     t_setup(number);
  97.     item_select = qshowstr(select_allowed);
  98.     t_endup(number);
  99.     return( item_select );
  100.     }
  101.  
  102. static int qshowstr(select_allowed)
  103. char select_allowed;
  104.     {
  105.     register int i,j,k,sigsav,itemselect=0;
  106.  
  107.     srcount=0;
  108.     sigsav=nosignal;
  109.     nosignal=1; /* don't allow ^c etc */
  110.     if (c[GOLD])
  111.         {
  112.         lprintf(".)   %d gold pieces",(long)c[GOLD]);
  113.         srcount++;
  114.         }
  115.     for (k=(MAXINVEN-1); k>=0; k--)
  116.       if (iven[k])
  117.         {
  118.         for (i=22; i<84; i++)
  119.              for (j=0; j<=k; j++)
  120.                  if (i==iven[j])
  121.              {
  122.              itemselect = show2(j);
  123.              if (itemselect && select_allowed)
  124.             goto quitit;
  125.              }
  126.         k=0;
  127.         }
  128.  
  129.     lprintf("\nElapsed time is %d.  You have %d mobuls left",(long)((gtime+99)/100+1),(long)((TIMELIMIT-gtime)/100));
  130.     itemselect = more(select_allowed);     
  131. quitit:
  132.     nosignal=sigsav;
  133.     if (select_allowed)
  134.     return( (itemselect > 0) ? itemselect : 0 );
  135.     else
  136.     return( 0 );
  137.     }
  138.  
  139. /*
  140.  subroutine to clear screen depending on # lines to display
  141.  
  142. */
  143. static void t_setup(count)
  144. register int count;
  145.     {
  146.     if (count<20)  /* how do we clear the screen? */
  147.         {
  148.         cl_up(79,count);
  149.         cursor(1,1);
  150.         }
  151.     else
  152.         {
  153.         resetscroll();
  154.         clear();
  155.         }
  156.     }
  157.  
  158. /*
  159.  subroutine to restore normal display screen depending on t_setup()
  160.  
  161. */
  162. static void t_endup(count)
  163. register int count;
  164.     {
  165.     if (count<18)  /* how did we clear the screen? */
  166.         draws(0,MAXX,0,(count>MAXY) ? MAXY : count);
  167.     else
  168.         {
  169.         drawscreen(); 
  170.         setscroll();
  171.         }
  172.     }
  173.  
  174. /*
  175.     function to show the things player is wearing only
  176.  */
  177. showwear()
  178.     {
  179.     register int i,j,sigsav,count,itemselect=0;
  180.  
  181.     sigsav=nosignal;  nosignal=1; /* don't allow ^c etc */
  182.     srcount=0;
  183.  
  184.     for (count=2,j=0; j< MAXINVEN; j++)   /* count number of items we will display */
  185.         if (i=iven[j])
  186.             switch(i)
  187.             {
  188.             case OLEATHER:  case OPLATE:    case OCHAIN:
  189.             case ORING:     case OSTUDLEATHER:  case OSPLINT:
  190.             case OPLATEARMOR:   case OSSPLATE:  case OSHIELD:
  191.             count++;
  192.             };
  193.  
  194.     t_setup(count);
  195.  
  196.     for (i=22; i<84; i++)
  197.          for (j=0; j< MAXINVEN; j++)
  198.            if (i==iven[j])
  199.             switch(i)
  200.                 {
  201.                 case OLEATHER:  case OPLATE:    case OCHAIN:
  202.                 case ORING:     case OSTUDLEATHER:  case OSPLINT:
  203.                 case OPLATEARMOR:   case OSSPLATE:  case OSHIELD:
  204.                 if (itemselect = show2(j))
  205.             goto quitit;
  206.                 };
  207.     itemselect = more(TRUE);     
  208. quitit:
  209.     nosignal=sigsav;    
  210.     t_endup(count);
  211.     return( (itemselect > 1) ? itemselect : 0 );
  212.     }
  213.  
  214. /*
  215.     function to show the things player can wield only
  216.  */
  217. showwield()
  218.     {
  219.     register int i,j,sigsav,count,itemselect=0;
  220.     sigsav=nosignal;  nosignal=1; /* don't allow ^c etc */
  221.     srcount=0;
  222.  
  223.      for (count=2,j=0; j< MAXINVEN; j++)  /* count how many items */
  224.        if (i=iven[j])
  225.         switch(i)
  226.             {
  227.             case ODIAMOND:  case ORUBY:  case OEMERALD:  case OSAPPHIRE:
  228.             case OBOOK:     case OCHEST:  case OLARNEYE: case ONOTHEFT:
  229.             case OSPIRITSCARAB:  case OCUBEofUNDEAD:
  230.             case OPOTION:   case OSCROLL:  break;
  231.             default:  count++;
  232.             };
  233.  
  234.     t_setup(count);
  235.  
  236.     for (i=22; i<84; i++)
  237.          for (j=0; j< MAXINVEN; j++)
  238.            if (i==iven[j])
  239.             switch(i)
  240.                 {
  241.                 case ODIAMOND:  case ORUBY:  case OEMERALD:  case OSAPPHIRE:
  242.                 case OBOOK:     case OCHEST:  case OLARNEYE: case ONOTHEFT:
  243.                 case OSPIRITSCARAB:  case OCUBEofUNDEAD:
  244.                 case OPOTION:   case OSCROLL:  
  245.             break;
  246.                 default:  
  247.             if (itemselect = show2(j))
  248.             goto quitit;
  249.                 };
  250.     itemselect = more(TRUE);     
  251. quitit:
  252.     nosignal=sigsav;    
  253.     t_endup(count);
  254.     return( (itemselect > 1) ? itemselect : 0 );
  255.     }
  256.  
  257. /*
  258.  *  function to show the things player can read only
  259.  */
  260. showread()
  261.     {
  262.     register int i,j,sigsav,count,itemselect = 0;
  263.     sigsav=nosignal;  nosignal=1; /* don't allow ^c etc */
  264.     srcount=0;
  265.  
  266.     for (count=2,j=0; j< MAXINVEN; j++)
  267.         switch(iven[j])
  268.             {
  269.             case OBOOK: case OSCROLL:   count++;
  270.             };
  271.     t_setup(count);
  272.  
  273.     for (i=22; i<84; i++)
  274.          for (j=0; j< MAXINVEN; j++)
  275.            if (i==iven[j])
  276.             switch(i)
  277.                 {
  278.                 case OBOOK: case OSCROLL:
  279.             if (itemselect = show2(j))
  280.             goto quitit;
  281.                 };
  282.     itemselect = more(TRUE);
  283. quitit:
  284.     nosignal=sigsav;
  285.     t_endup(count);
  286.     return((itemselect > 1) ? itemselect : 0 );
  287.     }
  288.  
  289. /*
  290.  *  function to show the things player can eat only
  291.  */
  292. showeat()
  293.     {
  294.     register int i,j,sigsav,count,itemselect=0;
  295.     sigsav=nosignal;  nosignal=1; /* don't allow ^c etc */
  296.     srcount=0;
  297.  
  298.     for (count=2,j=0; j< MAXINVEN; j++)
  299.         switch(iven[j])
  300.             {
  301.             case OCOOKIE:   count++;
  302.             };
  303.     t_setup(count);
  304.  
  305.     for (i=22; i<84; i++)
  306.          for (j=0; j< MAXINVEN; j++)
  307.            if (i==iven[j])
  308.             switch(i)
  309.                 {
  310.                 case OCOOKIE:   
  311.             if (itemselect=show2(j))
  312.             goto quitit;
  313.                 };
  314.     itemselect = more(TRUE);     
  315. quitit:
  316.     nosignal=sigsav;    
  317.     t_endup(count);
  318.     return( (itemselect > 1) ? itemselect : 0 );
  319.     }
  320.  
  321. /*
  322.     function to show the things player can quaff only
  323.  */
  324. showquaff()
  325.     {
  326.     register int i,j,sigsav,count,itemselect=0;
  327.     sigsav=nosignal;  nosignal=1; /* don't allow ^c etc */
  328.     srcount=0;
  329.  
  330.     for (count=2,j=0; j< MAXINVEN; j++)
  331.         switch(iven[j])
  332.             {
  333.             case OPOTION:   count++;
  334.             };
  335.     t_setup(count);
  336.  
  337.     for (i=22; i<84; i++)
  338.          for (j=0; j< MAXINVEN; j++)
  339.            if (i==iven[j])
  340.             switch(i)
  341.                 {
  342.                 case OPOTION:
  343.             if (itemselect=show2(j))
  344.             goto quitit;
  345.                 };
  346.     itemselect = more(TRUE);
  347. quitit:
  348.     nosignal=sigsav;
  349.     t_endup(count);
  350.     return( (itemselect > 1 ) ? itemselect : 0 );
  351.     }
  352.  
  353. show1(idx,str2)
  354.     register int idx;
  355.     register char *str2[];
  356.     {
  357.         lprc('\n');
  358.         cltoeoln();
  359.     if (str2==0)
  360.         lprintf("%c)   %s",idx+'a',objectname[iven[idx]]);
  361.     else if (*str2[ivenarg[idx]]==0)
  362.         lprintf("%c)   %s",idx+'a',objectname[iven[idx]]);
  363.     else
  364.         lprintf("%c)   %s of%s",idx+'a',objectname[iven[idx]],str2[ivenarg[idx]]);
  365.     }
  366.  
  367. show3(index)
  368. register int index ;
  369.     {
  370.     srcount=0;
  371.     return( show2(index) );
  372.     }
  373.  
  374. static int show2(index)
  375. register int index;
  376.     {
  377.     register int itemselect = 0;
  378.  
  379.     switch(iven[index])
  380.         {
  381.         case OPOTION:   show1(index,potionname);  break;
  382.         case OSCROLL:   show1(index,scrollname);  break;
  383.  
  384.         case OLARNEYE:      case OBOOK:         case OSPIRITSCARAB:
  385.         case ODIAMOND:      case ORUBY:         case OCUBEofUNDEAD:
  386.         case OEMERALD:      case OCHEST:        case OCOOKIE:
  387.         case OSAPPHIRE:     case ONOTHEFT:      show1(index,(char **)0);  break;
  388.  
  389.         default:
  390.         lprc('\n');
  391.         cltoeoln();
  392.         lprintf("%c)   %s",index+'a',objectname[iven[index]]);
  393.         if (ivenarg[index]>0)
  394.             lprintf(" + %d",(long)ivenarg[index]);
  395.         else if (ivenarg[index]<0)
  396.             lprintf(" %d",(long)ivenarg[index]);
  397.         break;
  398.         }
  399.     if (c[WIELD]==index) lprcat(" (weapon in hand)");
  400.     if ((c[WEAR]==index) || (c[SHIELD]==index))  lprcat(" (being worn)");
  401.     if (++srcount>=22) 
  402.     { 
  403.     srcount=0; 
  404.     itemselect = more(TRUE); 
  405.     clear(); 
  406.     }
  407.     return( itemselect );
  408.     }
  409.  
  410. /*
  411.     function to put something in the players inventory
  412.     returns 0 if success, 1 if a failure
  413. */
  414. take(itm,arg)
  415.     int itm,arg;
  416.     {
  417.     register int i,limit;
  418. /*  cursors(); */
  419.     if ((limit = 15+(c[LEVEL]>>1)) > MAXINVEN)
  420.         limit=MAXINVEN;
  421.     for (i=0; i<limit; i++)
  422.         if (iven[i]==0)
  423.             {
  424.             iven[i] = itm;  ivenarg[i] = arg;  limit=0;
  425.             switch(itm)
  426.                 {
  427.                 case OPROTRING: case ODAMRING: case OBELT: limit=1;  break;
  428.                 case ODEXRING:      c[DEXTERITY] += ivenarg[i]+1; limit=1;  break;
  429.                 case OSTRRING:      c[STREXTRA]  += ivenarg[i]+1;   limit=1; break;
  430.                 case OCLEVERRING:   c[INTELLIGENCE] += ivenarg[i]+1;  limit=1; break;
  431.                 case OHAMMER:       c[DEXTERITY] += 10; c[STREXTRA]+=10;
  432.                                     c[INTELLIGENCE]-=10;    limit=1;     break;
  433.  
  434.                 case OORBOFDRAGON:  c[SLAYING]++;       break;
  435.                 case OSPIRITSCARAB: c[NEGATESPIRIT]++;  break;
  436.                 case OCUBEofUNDEAD: c[CUBEofUNDEAD]++;  break;
  437.                 case ONOTHEFT:      c[NOTHEFT]++;       break;
  438.                 case OSWORDofSLASHING:  c[DEXTERITY] +=5;   limit=1; break;
  439.                 };
  440.             lprcat("\nYou pick up:"); show3(i);
  441.             if (limit) bottomline();  return(0);
  442.             }
  443.     lprcat("\nYou can't carry anything else");  return(1);
  444.     }
  445.  
  446. /*
  447.     subroutine to drop an object  returns 1 if something there already else 0
  448.  */
  449. drop_object(k)
  450.     int k;
  451.     {
  452.     int itm;
  453.     if ((k<0) || (k>=MAXINVEN)) 
  454.     return(0);
  455.     itm = iven[k];  cursors();
  456.     if (itm==0) { lprintf("\nYou don't have item %c! ",k+'a'); return(1); }
  457.     if (item[playerx][playery])
  458.         { beep(); lprcat("\nThere's something here already"); return(1); }
  459.     if (playery==MAXY-1 && playerx==33) return(1); /* not in entrance */
  460.     item[playerx][playery] = itm;
  461.     iarg[playerx][playery] = ivenarg[k];
  462.     lprcat("\n  You drop:"); show3(k); /* show what item you dropped*/
  463.     know[playerx][playery] = 0;  iven[k]=0; 
  464.     if (c[WIELD]==k) c[WIELD]= -1;      if (c[WEAR]==k)  c[WEAR] = -1;
  465.     if (c[SHIELD]==k) c[SHIELD]= -1;
  466.     adjustcvalues(itm,ivenarg[k]);
  467.     dropflag=1; /* say dropped an item so wont ask to pick it up right away */
  468.     return(0);
  469.     }
  470.  
  471. /*
  472.     routine to tell if player can carry one more thing
  473.     returns 1 if pockets are full, else 0
  474. */
  475. pocketfull()
  476.     {
  477.     register int i,limit; 
  478.     if ((limit = MIN_LIMIT + (c[LEVEL]>>1) ) > MAXINVEN )  
  479.     limit = MAXINVEN;
  480.     for (i=0; i<limit; i++) 
  481.     if (iven[i]==0) 
  482.         return(0);
  483.     return(1);
  484.     }
  485.